import Vue from 'vue'
import {getStorageSync} from "../api/wechat";
import {AESIV, AESKEY} from "./index";
import CryptoJS from 'crypto-js'
export const utils = {
  getElement:(id = "",callback )=>{
    let _query = wx.createSelectorQuery();
    _query.select(id).boundingClientRect();
    _query.exec((res) =>{
      callback && callback(res);
    })
  },
  jsonSort:(jsonObj) => {
    let arrObj = [];
    Object.keys(jsonObj).sort().map(function(i){
      arrObj.push(`${i}=${jsonObj[i]}`);
    });
    return arrObj.join('&');
  },
  enCrypt:(str) => {
    return CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(str), AESKEY, { iv: AESIV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }).toString();
  },
  getAlbum:(type = "" , callback) => {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: [type], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        callback && callback(res.tempFilePaths[0]);
      }
    })
  },
  getUserInfo:(cb) => {
    let user = getStorageSync('user');
    cb && cb(user);
  },
  arrayFormats(input,changes){
    let  toRawType = value => {
      return Object.prototype.toString.call(value).slice(8, -1);
    }
    let type = toRawType(input);
    if(type == 'Array'){
      return input.map( (item,i)=> {
        return utils.arrayFormats(item,changes);
      });

    } else if(type == 'Object'){
      for(let key  in input){
        input[key] = utils.arrayFormats(input[key],changes);
      }
      return input;
    } else {
      console.log();
      return changes[input + ''] != undefined ? changes[input] : input;
    }




  }
}
